<template>
  <!-- 转场太生硬用动画 -->
  <!-- 在transition里面写apper特性设置开启在初始化渲染的过渡 -->
  <transition appear name="slide">
    <music-list :songs="songs" :title="title" :bg-image="bgImage"></music-list>
  </transition>
</template>

<script>
// vuex的mapGetters语法糖
import {mapGetters} from 'vuex'
// 调用获取后端数据的方法getSingerDetail
import {getSingerDetail} from 'api/singer'
// 获取一个常量
import {ERR_OK} from 'api/config'
// 把改变数据结构的函数调用过来
// isValidMusic,processSongsUrl
import {createSong, isValidMusic, processSongsUrl} from 'common/js/song'
// 引入music-list这个子组件
import MusicList from 'components/music-list/music-list'

export default {
  data () {
    return {
      // 用来装改造后的数据结构
      songs: []
    }
  },
  computed: {
    title () {
      // 输出singer这个计算属性的name属性
      return this.singer.name
    },
    bgImage () {
      return this.singer.avatar
    },
    // 里面是个数组,用字符串来取到里面的值
    ...mapGetters([
      'singer'
    ])
  },
  created () {
    this._getDetail()
  },
  methods: {
    // jsonp请求后端包裹函数
    _getDetail () {
      // 如果页面直接刷新，就让页面回退
      if (!this.singer.id) {
        this.$router.push('/singer')
        return
      }
      getSingerDetail(this.singer.id).then((res) => {
        if (res.code === ERR_OK) {
          // 获取到数据，然后改造数据结构放入this.songs数组中
          // this.songs = this._normalizeSongs(res.data.list)
          processSongsUrl(this._normalizeSongs(res.data.list)).then((songs) => {
            this.songs = songs
          })
        }
      })
    },
    // 改造成我们需要的数据结构,拿到ret
    _normalizeSongs (list) {
      let ret = []
      list.forEach((item) => {
        let {musicData} = item
        // if (musicData.songid && musicData.albummid)
        if (isValidMusic(musicData)) {
          // 把每项符合条件的，然后改造后添加到数组中
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  },
  components: {
    MusicList
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
